package com.paireach.bw.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.TextView;

import com.blankj.utilcode.utils.NetworkUtils;
import com.dd.CircularProgressButton;
import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;

import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2017/2/16.
 */

public class SafeCheckActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "SafeCheckActivity";
    private Context context;
    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private CheckBox cb4;
    private CheckBox cb5;
    private CheckBox cb6;
    private CheckBox cb7;
    private CheckBox cb8;
    private CheckBox cb9;
    private CheckBox cb10;

    //    private Button btn;
    private CircularProgressButton btn;
    private Toolbar toolbar;
    private boolean isChecked1;
    private boolean isChecked2;
    private boolean isChecked3;
    private boolean isChecked4;
    private boolean isChecked5;
    private boolean isChecked6;
    private boolean isChecked7;
    private boolean isChecked8;
    private boolean isChecked9;
    private boolean isChecked10;
    private String tel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition slide_bottom = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_bottom);
            getWindow().setEnterTransition(slide_bottom);
        }
        setContentView(R.layout.activity_safe_check);
        context = this;
        tel = MyApplication.getSpUtils().getString(Constant.TEL, "");
        findViews();
        initViews();
    }

    private void initViews() {
        btn.setIndeterminateProgressMode(true);
        btn.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        toolbar.setTitle("安全条款确认");
    }

    private void findViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        cb1 = (CheckBox) findViewById(R.id.check1);
        cb2 = (CheckBox) findViewById(R.id.check2);
        cb3 = (CheckBox) findViewById(R.id.check3);
        cb4 = (CheckBox) findViewById(R.id.check4);
        cb5 = (CheckBox) findViewById(R.id.check5);
        cb6 = (CheckBox) findViewById(R.id.check6);
        cb7 = (CheckBox) findViewById(R.id.check7);
        cb8 = (CheckBox) findViewById(R.id.check8);
        cb9 = (CheckBox) findViewById(R.id.check9);
        cb10 = (CheckBox) findViewById(R.id.check10);
//        btn = (Button) findViewById(R.id.btn_commit);
        btn = (CircularProgressButton) findViewById(R.id.btn_commit);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_commit:
                if (NetworkUtils.isConnected()) {


                    int progress = btn.getProgress();
                    if (progress == -1) {
                        btn.setProgress(0);
                    } else if (progress == 0) {
                        isChecked1 = cb1.isChecked();
                        isChecked2 = cb2.isChecked();
                        isChecked3 = cb3.isChecked();
                        isChecked4 = cb4.isChecked();
                        isChecked5 = cb5.isChecked();
                        isChecked6 = cb6.isChecked();
                        isChecked7 = cb7.isChecked();
                        isChecked8 = cb8.isChecked();
                        isChecked9 = cb9.isChecked();
                        isChecked10 = cb10.isChecked();
                        if (isChecked1 && isChecked2 && isChecked3 && isChecked4 && isChecked5 && isChecked6 && isChecked7 && isChecked8 && isChecked9 && isChecked10) {
                            btn.setProgress(50);
                            new WayBillRequest().commitSafeCheck(tel, isChecked1, isChecked2, isChecked3, isChecked4, isChecked5, isChecked6, isChecked7, isChecked8, isChecked9,
                                    isChecked10);
                        } else {
                            showToast("请全部检查并打钩");
                        }
                    }
                } else {
                    showToast("网络连接不可用，请稍后重试");
                }
                break;
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.COMMIT_SAFE_CHECKED) {
            jsonParse(event.getJson());
        }
    }

    private void jsonParse(String json) {
        ReturnInfo info = GsonUtil.jsonToBean(json, ReturnInfo.class);
//        Toasty.success(context, info.getMsg()).show();
        showSuccess(info.getMsg());
        if (info.getStatus() == 1) {
            btn.setProgress(100);
            onBackPressed();

            AppEvent event2 = new AppEvent();
            event2.setExtraInfo("已经安全检查过了");
            EventBus.getDefault().post(event2);
        } else {
            btn.setProgress(0);
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.COMMIT_SAFE_CHECKED) {
            showToast("提交失败");
            btn.setProgress(-1);
        }
    }
}
